package oceanlife;
import java.awt.Color;
import java.awt.Component;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

import oceanlife.log.Logger;
import oceanlife.pathfinding.Line2;
import oceanlife.pathfinding.Obstacles;
import oceanlife.pathfinding.Pillar;
import oceanlife.pathfinding.Pnt;
import oceanlife.pathfinding.SimpleGraphBuilder;


public class PlaybackWorker extends SwingWorker<Integer, Integer>{
		private SimpleSimPanel simPanel;
		private BufferedReader poslog = null;
		private JLabel info;


		public PlaybackWorker(SimpleSimPanel simPanel, JLabel info, BufferedReader poslog) {
			this.info = info;
			this.simPanel = simPanel;
			this.poslog = poslog;
		}

		protected Integer doInBackground() throws Exception
        {
			try {	
				Date dateNow = new Date ();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd...HH_mm_ss.SSS");
				StringBuilder dateString = new StringBuilder( sdf.format( dateNow ) );
				System.out.println("Started_at: "+dateString.toString());
				String line = "";
				if(!poslog.ready()){
	    			OLSet.setInt("stopping", 1);
	    		} else {
					line = poslog.readLine();
	    		}
				outer:
				while(OLSet.getInt("stopping") != 1){
					line = poslog.readLine();
					java.util.Date javaDate = new java.util.Date();
				    long start = javaDate.getTime();
					ArrayList<Agent> agents = new ArrayList<Agent>();
				    
				    while(poslog.ready() && !line.equals("Step") && !line.equals("")){
				    	String[] split = line.split(" ");
				    	if(split.length != 6){
				    		break outer;
				    	}
				    	UUID id = UUID.fromString(split[0]);
				    	Pnt pos = new Pnt(split[1]);
				    	Pnt goal = new Pnt(split[2]);
				    	double freeSpeed = Double.parseDouble(split[3]);
				    	double direction = Double.parseDouble(split[4]);
				    	Color color = new Color(Integer.parseInt(split[5]));
			    		agents.add(new Agent(id,pos,goal,freeSpeed,direction,color));
				    	line = poslog.readLine();
				    }
				    
				    javaDate = new java.util.Date();
				    long end = javaDate.getTime();
				    double cps = OLSet.getCps();
				    int tpc = (int) Math.round(1000/cps);
					//System.out.println((end-start)+" "+(tpc-(end-start))+" "+tpc+" "+(float)(end-start)/(float)tpc);
				    info.setText(Float.toString((float)(end-start)/(float)tpc*(float)100.0));
				    if(tpc > (end-start)){
				    	Thread.sleep(tpc-(end-start));
				    }
		    		if(!poslog.ready()){
		    			OLSet.setInt("stopping", 1);
		    		}
		    		//System.out.println(agents.size());
				    simPanel.setAgents(agents);
					simPanel.repaint();
				}
				dateNow = new Date ();
				sdf = new SimpleDateFormat("yyyy.MM.dd...HH_mm_ss.SSS");
				dateString = new StringBuilder( sdf.format( dateNow ) );
				System.out.println("Stopped_at: "+dateString.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
        }


        protected void done()
        {
                try
                {
                	poslog.close();
                }
                catch (Exception e)
                {
                	e.printStackTrace();
                }
        }


}